<?php

namespace App\Http\Controllers\Admin\User;

use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Models\StoreCouponUser;
use App\Models\User;
use App\Models\UserBill;
use App\Models\UserSign;
use App\Traits\Controllers\AdminTraits;
use App\Transformers\IntegralLogTransFormer;
use App\Transformers\Order\ConsumptionOrderTransformer;
use App\Transformers\StoreCoupon\StoreCouponUserTransformer;
use App\Transformers\User\UserBillsTransformer;
use App\Transformers\User\UserDetailsTransformer;
use App\Transformers\User\UserSignTransFormer;
use App\Transformers\User\UserTransformer;

class UserDetailscontroller extends Controller
{
    //使用用户模型
    public $model = User::class;

    public $transformer = UserTransformer::class;

    //显示用户订单的方法
    public function userorder($id)
    {
        //获取到当前所传来的用户的 余额 积分 和总的订单次数
        $order = $this->getQuery()
            ->select('balance', 'integral', 'pay_count', 'avatar', 'nickname')
            ->where('id', $id)
            ->first()->toArray();
        //获取到用户本月的订单总金额
        $order1 = Order::query()
            ->where('user_id', $id)
            ->where('paid', 1)
            ->where('pay_time', '>=', date('Y-m-1', time()))
            ->where('pay_time', '<=', date('Y-m-t', time()))
            ->sum('pay_price');
        //获取用户本月的订单的总数
        $order_num = Order::query()
            ->where('user_id', $id)
            ->where('paid', 1)
            ->where('pay_time', '>=', date('Y-m-1', time()))
            ->where('pay_time', '<=', date('Y-m-t', time()))
            ->count();
        //获取用户的总的消费金额
        $order_price = Order::query()
            ->where('user_id', $id)
            ->where('paid', 1)
            ->sum('pay_price');
        //将获取到的数据追加到数组中
        $order['total'] = $order1;
        $order['m_total'] = $order_price;
        $order['m_order'] = $order_num;
        //返回查询到的数据
        return $this->response->array($order);
    }

    //查询单个用户得消费记录的方法
    public function recordsconsumption($id)
    {
        //查询订单表中得数据
        $order = Order::query()
            //要查询得字段
            ->select('id', 'order_number', 'user_name', 'total_num',
                'total_price', 'pay_price', 'pay_time', 'created_at', 'updated_at')
            ->where('user_id', $id)  //查询传来得用户得订单
            ->paginate($this->getPageLimit());//进行分页显示
        return $this->response->paginator($order, new ConsumptionOrderTransformer());
    }

    //查询单个用户的签到记录信息
    public function usersigns($id)
    {
        //查询用户签到记录表
        $usersigin = UserSign::query()
            //查询用户为当前传来的要搜索的用户
            ->where('user_id', $id)
            ->paginate($this->getPageLimit()); //进行分页显示
        //返回查询到的签到记录的信息
        return $this->response->paginator($usersigin, new UserSignTransFormer());
    }

    //查询当前用户所拥有的优惠券的
    public function usercoupon($id)
    {
        //查询当前用户所用户的优惠券
        $usercoupon = StoreCouponUser::query()
            ->where('user_id', $id)
            ->paginate($this->getPageLimit());
        //返回查询到的数据
        return $this->response->paginator($usercoupon, new StoreCouponUserTransformer());
    }

    //查询单个用户余额变动信息
    public function userbalance($id)
    {
        $userbill = UserBill::query()
            ->where('user_id', $id)
            ->paginate($this->getPageLimit());
        //返回查询到得数据
        return $this->response->paginator($userbill, new UserBillsTransformer());
    }

    //查询好友关系
    public function userfriend($id)
    {
        //同过用户模型关联中得   userfriends 方法查询到用户得关联得好友得信息 查找的id为传来的用户的id
        $user = $this->getQuery()
            ->find($id) //使用当前的模型查询到
            ->userfriends()
            ->paginate($this->getPageLimit());
        //返回查询到得数据
        return $this->response->paginator($user, new UserDetailsTransformer());
    }

    //获取单个用户积分明细
    public function userintegral($id)
    {
        $user = $this->getQuery()
            ->find($id)
            ->userIntegral()
            ->paginate($this->getPageLimit());
        return $this->response->paginator($user,new IntegralLogTransFormer());
    }

    /**
     * 获取一页的数量
     */
    public function getPageLimit()
    {
        $limit = request()->input('pageSize');

        if ($limit < 0 || empty($limit)) {
            $limit = config('paginate.limit');
        } elseif ($limit > 100) {
            $limit = 100;
        }

        return $limit;
    }

}
